/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","bsMsgServ", "ckeditor-jquery", "init","btnServ","bsButton","formServ","codeDef",
	"panelUtil","buttonEvent"],
		function($,datagridServ, bsMsgServ, ckeditor,init, btnServ,bsButton,formServ,codeDef,
			panelUtil,buttonEvent) {

			'use strict';

			var _moduleConfig,
				
				saveUrl = '/oaAddressList/save.action',
				
				editUrl='/oaAddressList/query/editPage.action',
				
				//页面根节点dom元素
				rootid = 'bs-address-list',
				
				//弹窗根节点dom元素
				edit_window_id = 'bs-address-list-win',
				
				//模块代码
				moduCode = 'oaAddressList',
				
				//模块配置
				_moduleConfig,				
				
				//部门编码
				deptid
				
				
				
			var public_method = {
				
				
				init:function(doc){
					private_method.init(doc);
				},
				destroy:function(){
					
					
					var window_btn = $('#'+ edit_window_id +' .function-btn')
					if(window_btn.length>0){
						window_btn.unbind('click')
					}
					
					
					var file_btn = $('#'+edit_window_id +' .file_upload_btn')
					if(file_btn.length>0){
						file_btn.unbind('click')
						$('#'+ edit_window_id).empty()
					}
					
					_moduleConfig = null;
					
					
					
					$('#'+ edit_window_id).empty();
				},
				/**
				 * 
				 * @param _options.grid	  文档列表
				 * @param _options.flag	  新增或编辑标志
				 * @param _options.window	编辑窗口
				 * @param _options.deptid 分类id
				 * @param _options.evt		触发事件
				 */
				pageShow:function(_options){					
					
					deptid = _options.deptid;
					var _df = {
						grid:$('#'+rootid + ' .dg-list'),
						window:$('#'+edit_window_id),
						onClose:public_method.destroy
					}
					var _default = $.extend({},_df,init.getWidthHeight(),_options)
					
											  
					
					_default = $.extend({},_default,
						{						
							href:'/'+init.getSite() + editUrl,					
							existCheckElement:'form'					
						}
					)
					
					
					datagridServ.windowEdit(_default);
					if(_options.evt)
						$(_options.evt.currentTarget).removeClass('disabled');
				}
				
			}

			var util = {
				getConfig:function(){
					
					if(!_moduleConfig){
						_moduleConfig = init.getBizConfig({bizcode:moduCode})
					}
					return _moduleConfig
				}
			}
			
			var private_method = {
				
				
				
				init : function(_options) {
					if (window.console) {
					console.info('bsAddressEdit loaded...');
					}
					
					var _config = util.getConfig() ;				
					
					
					//初始化编辑页面按钮
					private_method.initDetailEditBtn();
					
					
					
					//定位分类和明细dom对象
					var $typegrid = $('#'+rootid + ' .tg-tree');
					var $grid = $('#'+rootid + ' .dg-list');
					
					//定位form对象
					var $form = $('#'+edit_window_id + ' .main-form')								
					
					var selectedRecord=$grid.datagrid('getSelected');
					if(selectedRecord){
						$form.form('load',selectedRecord);						
					}									
					
					
					
					
					//绑定按钮事件
					private_method.bindBtnEvent();
				},			
				
				initDetailEditBtn:function(){
					if(window.console){
						console.log(' detail edit button init ...')
					}
					var _btn = bsButton.getBtn({bizcode:moduCode,btnType:'edit',bizType:2});
					
					if(_btn){
						var _c = $('#'+edit_window_id + ' .btn-container')
						if(_c.length>0){
							_c.empty();
							_c.append(_btn);
						}
					}
					
				}				
				,				
				//绑定按钮事件
				bindBtnEvent:function(){
					
					var $edit_window = $('#'+ edit_window_id);
					
					var window_btn = $('#'+edit_window_id + ' button')
					if(window_btn.length>0){
						window_btn.unbind('click')
						window_btn.bind('click',private_method.function_btn_listenner)
					}				
					
				},	
				
				//功能按钮监听函数
				function_btn_listenner:function(e){	
					init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});									
				}				
				
			}
			var btnEvent = {
				// 提交form
				editSave : function(e) {
					
					var _f = $('#'+edit_window_id + ' .main-form')
					
					
					var $edit_window = $('#'+edit_window_id);
					
					
					
					var formData = formServ.getDataObj({
								form : $('#'+edit_window_id + ' .main-form')
							});
					
					//设置分类编码
					if(deptid){
						formData.deptid = deptid;
					}
					
					
					
					$.ajax({
						url:'/'+init.getSite()+'/oaAddressList/save.action',
						data:$.toJSON(formData),
						contentType:'appliction/json;charset=utf-8',
						type:'post',
						dataType:'json',
						success:function(data){
							if(data){
								//刷新grid
								$('#'+rootid + ' .dg-list').datagrid('reload')
								//刷新form
								$('#'+edit_window_id + ' .main-form').form('load',data);
							}
							bsMsgServ.msgSlide(codeDef.getConfig({
								type:'operate',
								code:'success'
							}))
							
							$(e.currentTarget).removeClass('disabled');
						}
						,
						failure:function(){
							bsMsgServ.msgSlide(codeDef.getConfig({
								type:'operate',
								code:'error'
							}))
							$(e.currentTarget).removeClass('disabled');
						}
					})
									
				},				
				
				//
				cancel : function() {
					$('#'+edit_window_id).window('close');
				}
				

			};

			
			return public_method;

		});